const app = getApp()

Page({
  data: {
    schoollistifo: [],
  },

  onLoad: function (options) {
    var that = this;
    
    this.getschoollist()
  },

  onReady: function () {

  },

  onShow: function () {
  },
  /**
  * 生命周期函数--监听页面隐藏
  */
  onHide: function () {
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  },
  // 获取 学校列表
  getschoollist: function () {
    var that = this;
    if (wx.getStorageSync('schoollistifo') != '') {
      that.setData({
        schoollistifo: wx.getStorageSync('schoollistifo'),
        bindcshool: wx.getStorageSync('cop_id')
      })
    }
    else
    {
      wx.showLoading({
        title: '加载中',
      })
      var data={
        'open_id': wx.getStorageSync('openId')
      }
      app.post(app.globalData.serverurl + '/api/user/schoollist', data).then((res) => {
        console.log(res);//正确返回结果
        wx.setStorageSync('schoollistifo', res)
        that.setData({
          schoollistifo: res,
        })
        wx.hideLoading()

      })
    }
  },

// 点击学校后 如果已经绑定 跳转 my 页面 如果没有绑定 跳转 绑定页面
  choiceschool: function (e) {
    var cop_id = e.currentTarget.dataset.cop_id
    var cop_name = e.currentTarget.dataset.cop_name
    console.log(cop_id)
    console.log(cop_name)
    var data={
      'open_id':wx.getStorageSync('openId'),
      'cop_id': e.currentTarget.dataset.cop_id
    }
    app.post(app.globalData.serverurl + '/api/user/bindselect', data).then((res) => {
      console.log(res);//正确返回结果
      if (res.status =='bind')
      {
        wx.navigateTo({
          url: '../../pages/schoolbind/schoolbind?cop_id=' + cop_id + '&schoolname=' + cop_name + '&userid=' + res.userid + '&name=' + res.name,
      })
      }
      else{
        wx.navigateTo({
        url: '../../pages/bind/bind?cop_id='+cop_id+'&schoolname='+cop_name,
      })
      }
    })
  }

})